﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using CM.BL.BE;
using Newtonsoft.Json;
using CM.BL.BC;

namespace WebAppHTML5
{
    public partial class item : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string pRetval = "";

            if (!Request.HttpMethod.Equals("POST"))
            {
                
            }
            else
            {
                pRetval = "{\"success\":{0}, \"errors\":{\"reason\": \"{1}\"},  ";
                pRetval += "\"obj\": {2}, \"itm\": {3} ";
                pRetval += "}";

                string _obj = "";
                string _itm = "";
                string result = itemx(out _obj, out _itm);
                if (result != "")
                {
                    pRetval = pRetval.Replace("{0}", "false");
                    pRetval = pRetval.Replace("{1}", result);
                    pRetval = pRetval.Replace("{2}", _obj);
                    pRetval = pRetval.Replace("{3}", _itm);
                }
                else
                {
                    pRetval = pRetval.Replace("{0}", "false");
                    pRetval = pRetval.Replace("{1}", result);
                    pRetval = pRetval.Replace("{2}", "null");
                    pRetval = pRetval.Replace("{3}", "null");
                }
            }

            Response.ContentType = "application/json; charset=UTF-8";
            Response.Write(pRetval);
        }

        private string itemx(out string _pobj, out string _itm)
        {
            string retval = string.Empty; string po = string.Empty, p = string.Empty;
            string _retval = "";
            po = Request.Params["po"].ToString();
            p = Request.Params["p"].ToString();
            
            List<PurchaseOrderBE> lstPO = new List<PurchaseOrderBE>();
            retval = PurchaseOrderBC.getInstance(Session.SessionID).getItemsForRelease(out lstPO);
            PurchaseOrderBE pObj = null;
            POItemBE itm = null;
            string obj = ""; 
            string item = "";

            if (lstPO == null || lstPO.Count == 0)
            {
                pObj = lstPO.Find(X => X.PO == po);
                itm = pObj.Items.Find(X => X.Pos == p);

                if (pObj != null)
                {
                    obj = JsonConvert.SerializeObject(pObj);
                    item = JsonConvert.SerializeObject(itm);
                }
                else
                {
                    obj = "";
                    item = "";
                }
            }

            _pobj = obj;
            _itm = item;

            return retval;
        }
    }
}
